package demo5;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:张熳
 * Date:2025-08-29
 * Time:1:38
 */
//Object类(equals、toString、hashcode方法)

//Java里面除了Object类，所有的类都是存在继承关系的。默认会继承Object父类。即所有类的对象都可以使用Object的引用进行接收。

//范例：使用Object接收所有类的对象
class Person{
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        //如果传入的对象就是当前对象本身，即引用指向同一个对象（引用同一个内存地址），就直接返回true，因为一个对象肯定等于自己
        if (obj == this) {
            return true;
        }
        //如果不是同一个类型，即obj这个引用没有引用Student这个对象，就返回false
        if (!(obj instanceof Student)) {
            return false;
        }
        Student tmp = (Student) obj;
        return tmp.name.equals(this.name) && tmp.age == this.age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class TestObject {
    //hashcode方法
    public static void main(String[] args) {
        Student student1 = new Student("小美",10);
        Student student2 = new Student("小美",10);
        System.out.println(student1.hashCode());
        System.out.println(student2.hashCode());

        Person person1 = new Person("小帅",13);
        Person person2 = new Person("小帅",13);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }
    //toString方法
    public static void main3(String[] args) {
        Student student = new Student("小明",13);
        System.out.println(student.toString());
        //System.out.println(student);
    }
    //equals方法
    public static void main2(String[] args) {
        Student obj1 = new Student("小敏",12);
        Student obj2 = new Student("小敏",12);

        Student student1 = new Student("小红",11);
        Student student2 = new Student("小红",11);

        System.out.println(obj1 == obj2);
        System.out.println(obj1.equals(obj2));
        System.out.println(student1.equals(student2));
    }
    public static void func(Object obj) {
        System.out.println(obj);
    }
    public static void main1(String[] args) {
        //func(new Person());
        //func(new Student());
    }
}
